//
//  ChatRoomSearchViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/12.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ChatRoomSearchViewController.h"
#import "ChatRoomModel.h"
#import "ChatRoomTableViewCell.h"
#import "MyChatViewController.h"

@interface ChatRoomSearchViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *mySearchBar;
@property (nonatomic, strong) UITableView *myChatTableView;
@property (nonatomic, strong) NSMutableArray *myChatDataArray; // 数据源数组
@property (nonatomic, assign) BOOL isHaveData; // 判断时候有数据

@end

@implementation ChatRoomSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // navi的修改
    [self changeNavigation];
    // 添加searchBar
    [self addSearchBar];
    // 添加数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:25]};
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 添加searchBar
- (void)addSearchBar
{
    self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260 * kAdjustParamater, 30 * kAdjustParamater)];
    self.mySearchBar.barStyle = UIBarStyleDefault;
    self.mySearchBar.delegate = self;
    self.mySearchBar.placeholder = @"输入聊天室的名字";
    self.mySearchBar.tintColor = kCyanColor;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260 * kAdjustParamater, 30 * kAdjustParamater)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [view addSubview:_mySearchBar];
    self.navigationItem.titleView = view;
}

#pragma mark - UISearchBar代理
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // 在每次搜索框的内容变化的时候初始化数据源数组
    self.myChatDataArray = [NSMutableArray array];
    
    // 构造临时数据
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病", @"_name", @"本聊天室是为心脏病而建立", @"_introduce" , nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"心肌梗塞", @"_name", @"本聊天室是为心肌梗塞而建立", @"_introduce" , nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"糖尿病", @"_name", @"本聊天室是为糖尿病而建立", @"_introduce" , nil];
    NSArray *tempArray = [NSMutableArray arrayWithObjects:dic1, dic2, dic3, nil];
    for (NSDictionary *tempDic in tempArray) {
        if ([[tempDic valueForKey:@"_name"] containsString:searchText]) {
            ChatRoomModel *model = [[ChatRoomModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.myChatDataArray addObject:model];
        }
    }
    if (self.myChatDataArray.count > 0) {
        // 说明有数据
        self.isHaveData = YES;
    } else {
        // 说明没有数据
        self.isHaveData = NO;
    }
    [self.myChatTableView reloadData];
}

// 取消键盘
-  (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

#pragma mark - 建立数据源
- (void)addData
{
    
}

#pragma mark - 布局
- (void)addMySubview
{
    self.myChatTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight * kAdjustParamater - 64) style:UITableViewStylePlain];
    self.myChatTableView.backgroundColor = [UIColor whiteColor];
    self.myChatTableView.dataSource = self;
    self.myChatTableView.delegate = self;
    [self.view addSubview:_myChatTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myChatDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myFriendCell";
    ChatRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ChatRoomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.joinChatRoomButton addTarget:self action:@selector(JoinChat:) forControlEvents:UIControlEventTouchUpInside];
    cell.joinChatRoomButton.tag = indexPath.row + 100;
    // 获得model
    cell.model = self.myChatDataArray[indexPath.row];
    return cell;
}

#pragma mark - 加入聊天室
- (void)JoinChat:(UIButton *)button
{
    NSInteger row = button.tag - 100;
    ChatRoomModel *model = self.myChatDataArray[row];
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_CHATROOM;
    myChatVC.title = model.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
    NSLog(@"名字：%@", model.name);
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * kAdjustParamater;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatRoomModel *model = self.myChatDataArray[indexPath.row];
    
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_CHATROOM;
    myChatVC.title = model.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
    NSLog(@"聊天室名字：%@", model.name);
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.isHaveData == YES) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 35 * kAdjustParamater)];
        view.backgroundColor = [UIColor whiteColor];
        
        UILabel *linkPeopleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16 * kAdjustParamater, 12 * kAdjustParamater, 150 * kAdjustParamater, 15 * kAdjustParamater)];
        //    linkPeopleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        linkPeopleLabel.text = @"聊天室搜索结果";
        linkPeopleLabel.textAlignment = NSTextAlignmentLeft;
        linkPeopleLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        linkPeopleLabel.font = [UIFont systemFontOfSize:k15Big];
        [view addSubview:linkPeopleLabel];
        
        UILabel *grayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, linkPeopleLabel.bottom + 7 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
        grayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        [view addSubview:grayLine];
        
        return view;
    }
    // 没有数据
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.isHaveData == YES) {
        return 35 * kAdjustParamater;
    }
    return 0;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
